package com.example.piece.ui.activity

import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import com.therouter.router.Route
import com.example.base.aop.click.RepeatClick
import com.example.base.ext.getViewModel
import com.example.base.ext.navigationTo
import com.example.base.ext.setCircleCropImageUrl
import com.example.base.ext.toast
import com.example.base.vm.onError
import com.example.piece.App
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivityUserInfoBinding
import com.example.piece.imageengine.GlideEngine
import com.example.piece.vm.UserViewModel
import com.github.gzuliyujiang.wheelpicker.BirthdayPicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener


/**
 * 用户信息编辑
 */
@Route(path = RoutePath.MAIN_USER_INFO_ACTIVITY)
class UserInfoActivity : Layer3Activity<ActivityUserInfoBinding, ViewDataBinding>() {
    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = true
    override val leftIcon1Res: Int = R.drawable.ic_back
    override val centerLayoutRes: Int
        get() = R.layout.activity_user_info
    override val title: String
        get() = "用户信息"
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }

    override fun onClickLeftIcon1(view: View) {
        onBackPressed()

    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mCenterViewBinding?.userInfo = App.userLoginCodeEntity
    }

    //更换头像
    @RepeatClick
    fun changePhoto(view: View) {
        PictureSelector.create(this)
            .openGallery(SelectMimeType.ofImage())
            .setMaxSelectNum(1)
            .setImageEngine(GlideEngine.createGlideEngine())
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: ArrayList<LocalMedia?>?) {
                    if (result != null && result.size > 0) {
                        mCenterViewBinding?.imgHead?.setCircleCropImageUrl(result.get(0)?.availablePath)
                    }
                }

                override fun onCancel() {}
            })
    }

    //更换昵称
    @RepeatClick
    fun changeNickname(view: View) {
        navigationTo(RoutePath.MAIN_UPDATE_NICKNAME_SIGN_ACTIVITY, mutableMapOf(
            Key.DataKey.H5_TITLE to "修改昵称",
            Key.DataKey.CHANGE_USER_INFO_CONTENT to App.userLoginCodeEntity.user.nickname,
            Key.DataKey.CHANGE_USER_INFO_TYPE to 0
        ))

    }

    override fun onResume() {
        super.onResume()
        mCenterViewBinding?.userInfo = App.userLoginCodeEntity

    }

    //更换签名
    @RepeatClick
    fun changeSign(view: View) {
        navigationTo(RoutePath.MAIN_UPDATE_NICKNAME_SIGN_ACTIVITY, mutableMapOf(
            Key.DataKey.H5_TITLE to "修改签名",
            Key.DataKey.CHANGE_USER_INFO_CONTENT to App.userLoginCodeEntity.user.signature,

            Key.DataKey.CHANGE_USER_INFO_TYPE to 1
        ))

    }

    //更换性别
    @RepeatClick
    fun changeSex(view: View) {

        val picker = OptionPicker(this)
        picker.setData("男", "女")
        picker.setDefaultValue(App.userLoginCodeEntity?.user?.sex)
        var content = ""
        picker.setOnOptionPickedListener { position, item ->
            when (position) {
                0 -> {
                    content = "0"
                }
                1 -> {
                    content = "1"

                }
            }

            userViewModel.userInfoUpdate(this,
                content,
                ("3")) {
                if (it.code == 200) {
                    toast("修改成功")

                } else {
                    toast(it.msg)

                }
                App.userLoginCodeEntity?.user?.sex = if (content.toInt() == 0) "男" else "女"
                mCenterViewBinding?.userInfo = App.userLoginCodeEntity

            } onError {
                toast(it)
            }
        }
        picker.show()
    }

    //更换生日
    @RepeatClick
    fun changeBirthday(view: View) {
        val birthday = App.userLoginCodeEntity?.user?.birthday?.split("-")

        val picker = BirthdayPicker(this)
        if (birthday != null) {
            birthday?.let {
                if (it.size == 3) {
                    picker.setDefaultValue(it[0].toInt(), it[1].toInt(), it[2].toInt())

                }else{
                    picker.setDefaultValue(1991, 11, 11)

                }

            }
        } else {
            picker.setDefaultValue(1991, 11, 11)

        }

        picker.setOnDatePickedListener(object : OnDatePickedListener {
            override fun onDatePicked(year: Int, month: Int, day: Int) {
                var content = "$year-$month-$day"
                Log.e("onDatePicked", "onDatePicked: c" + content)
                userViewModel.userInfoUpdate(this@UserInfoActivity,
                    content,
                    ("4")) {
                    if (it.code == 200) {
                        toast("修改成功")

                    } else {
                        toast(it.msg)

                    }
                    App.userLoginCodeEntity?.user?.birthday = content
                    mCenterViewBinding?.userInfo = App.userLoginCodeEntity

                } onError {
                    toast(it)
                }
            }

        })
        picker.wheelLayout.setResetWhenLinkage(false)
        picker.show()
    }

    //我的邀请码
    @RepeatClick
    fun inventCode(view: View) {

        val cmb =
            view.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        cmb.text = App.userLoginCodeEntity.user.invitedCode
        Toast.makeText(view.context, "已经复制到粘贴板", Toast.LENGTH_SHORT).show()
    }

    //绑定邀请码
    @RepeatClick
    fun bindInventCode(view: View) {
    }
}